package com.faithbj.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.faithbj.common.configuration.Pager;
import com.faithbj.common.model.CheckTotal;
import com.faithbj.constant.Constant;
import com.faithbj.dao.CheckTotalDao;
import com.faithbj.dao.EntrustInfoDao;
import com.faithbj.dao.FamilyMemberDao;
import com.faithbj.service.CheckTotalService;

@Service("checkTotalService")
@Transactional
public class CheckTotalServiceImpl implements CheckTotalService {

	@Resource
	public CheckTotalDao checkTotalDao;

	@Resource
	public EntrustInfoDao entrustInfoDao;

	@Resource
	public FamilyMemberDao familyMemberDao;

	public Pager getCheckTotalListForApplyInfo(Pager pager) {

		Long totalCount = checkTotalDao.getTotalCount();
		pager.setTotalCount(totalCount.intValue());

		int from = (pager.getPageNumber()-1) * pager.getPageSize();

		List<CheckTotal> list = checkTotalDao
				.getCheckTotalListForApplyInfoByPage(from, pager.getPageSize());

		for(CheckTotal ct:list)
			ct.setMainFamilyMember();
		
		pager.setResult(list);


		return pager;
	}

	@Override
	public void updateCheckTotal(CheckTotal checkTotal) {
		// 更新核对总体情况
		checkTotalDao.saveOrUpdate(checkTotal);
	}


	@Override
	public CheckTotal getCheckTotalById(String id) {
		return checkTotalDao.get(id);
	}

	@Override
	public List<CheckTotal> queryCheckTotalListByCondition(Map condition) {
		// TODO Auto-generated method stub
		return checkTotalDao.queryCheckTotalListByCondition(condition);
	}

	@Override
	public Map<String, List<CheckTotal>> getCheckTotalMapForBatching(
			String[] acceptNums) {
		List<CheckTotal> ctList = checkTotalDao.queryListFoBatching(
				Constant.FALSE, acceptNums);
		Map<String, List<CheckTotal>> batchingMap = new HashMap<String, List<CheckTotal>>();
		if (ctList != null && !ctList.isEmpty()) {
			for (CheckTotal ct : ctList) {
				String regionId = ct.getRegionId();
				if (batchingMap.get(regionId) == null) {
					List<CheckTotal> batchingList = new ArrayList<CheckTotal>();
					batchingList.add(ct);
					batchingMap.put(regionId, batchingList);
				} else {
					batchingMap.get(regionId).add(ct);
				}
			}
		}
		return batchingMap;
	}
	
	@Override
    public Map<String, List<CheckTotal>> getCheckTotalMapForBatching() {
	       List<CheckTotal> ctList = checkTotalDao.queryListFoBatching(
	                Constant.FALSE);
	        Map<String, List<CheckTotal>> batchingMap = new HashMap<String, List<CheckTotal>>();
	        if (ctList != null && !ctList.isEmpty()) {
	            for (CheckTotal ct : ctList) {
	                String regionId = ct.getRegionId();
	                if (batchingMap.get(regionId) == null) {
	                    List<CheckTotal> batchingList = new ArrayList<CheckTotal>();
	                    batchingList.add(ct);
	                    batchingMap.put(regionId, batchingList);
	                } else {
	                    batchingMap.get(regionId).add(ct);
	                }
	            }
	        }
	        return batchingMap;
	}

    @Override
	public List<CheckTotal> queryAll() {
		// TODO Auto-generated method stub
		return checkTotalDao.getAll();
	}

	@Override
	public String save(CheckTotal entity) {
		return checkTotalDao.save(entity);
	}
	
}
